package com.itheima.test04;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*

创建服务器对象
接受数据
输入流
输入流保存到本地  创建输出流 写到服务器的

给 客户端响应
给结束标记
释放资源

 */
public class ServerDemo {

    public static void main(String[] args) throws IOException {
        //创建服务器对象
        ServerSocket ss = new ServerSocket(8888);
        //接受数据
        Socket socket = ss.accept();
        //输入流
        InputStream is = socket.getInputStream();
        //输入流保存到本地  创建输出流 写到服务器的
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day16\\copy\\meinv.png"));
        byte[] bytes= new byte[1024];
        int len;
        while ((len=is.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }

        //给 客户端响应
        OutputStream os = socket.getOutputStream();
        //字符-> 字节
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
        writer.write("上传成功!");
        writer.flush();
        //os.close()
        //给结束标记
        socket.shutdownOutput();

        //释放资源
        ss.close();
        bos.close();


    }
}
